/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class MenuCanvas extends Canvas {

    NinjaRunMidlet midlet;
    Display display;
    Image imgBg;
    Image imgTitleId;
    Image imgSelectSd;
    Image imgSearchSdOff;
    Image imgSearchSdOn;
    Image imgAboutIdOff;
    Image imgAboutIdOn;
    Image imgExitSdOff;
    Image imgExitSdOn;
//---
    Image imgTransparent;
    Image imgAlertExitSd;
    Image imgYesSdOff;
    Image imgYesSdOn;
    Image imgNoSdOff;
    Image imgNoSdOn;
    
    int x;
    int y;
    
    ButtonImage btnSelectSd;
    ButtonImage btnExitSd;
    ButtonImage btnAboutId;
    ButtonImage btnSearchSd;
    ButtonImage btnNo;
    ButtonImage btnYes;
    boolean showDialog = false;
    public static Font smallPlainFont;
    public static Font smallBoldFont;
    public static Font largeBoldFont;

    public MenuCanvas(NinjaRunMidlet midlet, Display display) {
        setFullScreenMode(true);

        this.midlet = midlet;
        this.display = display;

        
        try {
            imgBg = Image.createImage("/bg-snow640.png");
            
            imgSelectSd = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_select_sd.png");

            imgAboutIdOff = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_about_id_off.png");
            imgAboutIdOn = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_about_id_on.png");

            imgSearchSdOff = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_search_sd_off.png");
            imgSearchSdOn = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_search_sd_on.png");

            imgExitSdOff = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_exit_sd_off.png");
            imgExitSdOn = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_exit_sd_on.png");

            imgTransparent = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/transparent.png");
            imgAlertExitSd = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_alert_exit_sd.png");
            imgNoSdOff = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_no_sd_off.png");
            imgNoSdOn = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_no_sd_on.png");
            imgYesSdOff = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_yes_sd_off.png");
            imgYesSdOn = Image.createImage("/jtkpolban/kartikawindy/kasund/resources/sd_yes_sd_on.png");

        } catch (IOException ex) {
        }

        smallPlainFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        smallBoldFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        largeBoldFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);

        int xyArea[] = new int[4];
        

        int hExit = (int) (getWidth() * 0.75) - (imgExitSdOff.getWidth() / 2);
        int wExit = (int) (getHeight() * 0.7) - (imgExitSdOff.getHeight() / 2);
        int hAbout = (int) (getWidth() * 0.25) - (imgAboutIdOff.getWidth() / 2);
        int wAbout = (int) (getHeight() * 0.3) - (imgAboutIdOff.getHeight() / 2);
        int hSearch = (int) (getWidth() * 0.5) - (imgSearchSdOff.getWidth() / 2);
        int wSearch = (int) (getHeight() * 0.5) - (imgSearchSdOff.getHeight() / 2);

        int hNo = (int) (getWidth() * 0.5) + 5;
        int wNo = (int) (getHeight() * 0.5) + 10;
        int hYes = (int) (getWidth() * 0.5) - imgYesSdOff.getWidth() - 5;
        int wYes = (int) (getHeight() * 0.5) + 10;

        
        int hSelect = getWidth() / 2 - (imgSelectSd.getWidth() / 2);
        int wSelect = getHeight() - imgSelectSd.getHeight();
        xyArea[0] = wSelect; //x1
        xyArea[1] = wSelect + imgSelectSd.getWidth(); //x2
        xyArea[2] = hSelect; //y1
        xyArea[3] = hSelect + imgSelectSd.getHeight(); //y2
        btnSelectSd = new ButtonImage("selectSd", imgSelectSd, imgSelectSd,
                imgSelectSd, hSelect,
                wSelect, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = wExit; //x1
        xyArea[1] = wExit + imgExitSdOff.getWidth(); //x2
        xyArea[2] = hExit; //y1
        xyArea[3] = hExit + imgExitSdOff.getHeight(); //y2
        btnExitSd = new ButtonImage("contributeSd", imgExitSdOff, imgExitSdOn,
                imgExitSdOff, hExit,
                wExit, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = wAbout; //x1
        xyArea[1] = wAbout + imgAboutIdOff.getWidth(); //x2
        xyArea[2] = hAbout; //y1
        xyArea[3] = hAbout + imgAboutIdOff.getHeight(); //y2
        btnAboutId = new ButtonImage("learnSd", imgAboutIdOff, imgAboutIdOn,
                imgAboutIdOff, hAbout,
                wAbout, xyArea, Graphics.TOP | Graphics.LEFT);
        //btnExitSd.setPosition(hAbout, wAbout, xyArea,
        //        Graphics.TOP | Graphics.LEFT);

        xyArea[0] = wSearch; //x1
        xyArea[1] = wSearch + imgSearchSdOff.getWidth(); //x2
        xyArea[2] = hSearch; //y1
        xyArea[3] = hSearch + imgSearchSdOff.getHeight(); //y2
        btnSearchSd = new ButtonImage("searchSd", imgSearchSdOff, imgSearchSdOn,
                imgSearchSdOff, hSearch,
                wSearch, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = wNo; //x1
        xyArea[1] = wNo + imgNoSdOff.getWidth(); //x2
        xyArea[2] = hNo; //y1
        xyArea[3] = hNo + imgNoSdOff.getHeight(); //y2
        btnNo = new ButtonImage("noEn", imgNoSdOff, imgNoSdOn,
                imgNoSdOff, hNo,
                wNo, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = wYes; //x1
        xyArea[1] = wYes + imgYesSdOff.getWidth(); //x2
        xyArea[2] = hYes; //y1
        xyArea[3] = hYes + imgYesSdOff.getHeight(); //y2
        btnYes = new ButtonImage("yesEn", imgYesSdOff, imgYesSdOn,
                imgYesSdOff, hYes,
                wYes, xyArea, Graphics.TOP | Graphics.LEFT);

        btnSearchSd.setIsPressed(true);
    }

    /*
    protected void sizeChanged(int i, int i1) {
    super.sizeChanged(i, i1);
    //System.out.println("Oit!");
    System.out.println(getWidth() + "x" +getHeight());
    }
     *
     */
    protected void paint(Graphics g) {
        int xyArea[] = new int[4];

        g.setColor(0xFFFFFF);
        g.fillRect(0, 0, getWidth(), getHeight());
       
        /*
        if (getWidth() < getHeight()) {
            //System.out.println("portrait!");
            g.drawImage(imgBg240x320, 0, 0, Graphics.TOP | Graphics.LEFT);
        } else {
            //System.out.println("landscape!");
            g.drawImage(imgBg320x240, 0, 0, Graphics.TOP | Graphics.LEFT);
        }
         * 
         */
        g.drawImage(imgBg, 0, 0, Graphics.TOP | Graphics.LEFT);

        int hExit = (int) (getWidth() * 0.75) - (imgExitSdOff.getWidth() / 2);
        int wExit = (int) (getHeight() * 0.7) - (imgExitSdOff.getHeight() / 2);
        int hAbout = (int) (getWidth() * 0.25) - (imgAboutIdOff.getWidth() / 2);
        int wAbout = (int) (getHeight() * 0.3) - (imgAboutIdOff.getHeight() / 2);
        int hSearch = (int) (getWidth() * 0.5) - (imgSearchSdOff.getWidth() / 2);
        int wSearch = (int) (getHeight() * 0.5) - (imgSearchSdOff.getHeight() / 2);

        int hNo = (int) (getWidth() * 0.5) + 5;
        int wNo = (int) (getHeight() * 0.5) + 10;
        int hYes = (int) (getWidth() * 0.5) - imgYesSdOff.getWidth() - 5;
        int wYes = (int) (getHeight() * 0.5) + 10;
        
        int hSelect = getWidth() / 2 - (imgSelectSd.getWidth() / 2);
        int wSelect = getHeight() - imgSelectSd.getHeight();
        xyArea[0] = wSelect; //x1
        xyArea[1] = wSelect + imgSelectSd.getWidth(); //x2
        xyArea[2] = hSelect; //y1
        xyArea[3] = hSelect + imgSelectSd.getHeight(); //y2
        btnSelectSd = new ButtonImage("selectSd", imgSelectSd, imgSelectSd,
                imgSelectSd, hSelect,
                wSelect, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = wExit; //x1
        xyArea[1] = wExit + imgExitSdOff.getWidth(); //x2
        xyArea[2] = hExit; //y1
        xyArea[3] = hExit + imgExitSdOff.getHeight(); //y2
        btnExitSd.setPosition(hExit, wExit, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = wAbout; //x1
        xyArea[1] = wAbout + imgAboutIdOff.getWidth(); //x2
        xyArea[2] = hAbout; //y1
        xyArea[3] = hAbout + imgAboutIdOff.getHeight(); //y2
        btnAboutId.setPosition(hAbout, wAbout, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = wSearch; //x1
        xyArea[1] = wSearch + imgSearchSdOff.getWidth(); //x2
        xyArea[2] = hSearch; //y1
        xyArea[3] = hSearch + imgSearchSdOff.getHeight(); //y2
        btnSearchSd.setPosition(hSearch, wSearch, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = wNo; //x1
        xyArea[1] = wNo + imgNoSdOff.getWidth(); //x2
        xyArea[2] = hNo; //y1
        xyArea[3] = hNo + imgNoSdOff.getHeight(); //y2
        btnNo.setPosition(hNo, wNo, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = wYes; //x1
        xyArea[1] = wYes + imgYesSdOff.getWidth(); //x2
        xyArea[2] = hYes; //y1
        xyArea[3] = hYes + imgYesSdOff.getHeight(); //y2
        btnYes.setPosition(hYes, wYes, xyArea, Graphics.TOP | Graphics.LEFT);

        btnSearchSd.paint(g);
        btnAboutId.paint(g);
        btnExitSd.paint(g);
        btnSelectSd.paint(g);
        if (showDialog == true) {
            g.drawImage(imgTransparent, 0, 0, Graphics.TOP | Graphics.LEFT);
            drawImgCenter(g, imgAlertExitSd);
            btnNo.paint(g);
            btnYes.paint(g);
        }

    }

    protected void keyPressed(int keyCode) {
        //System.out.println(keyCode);
        if (keyCode == -5) {
            if (showDialog) {
                if (btnYes.isIsPressed()) {
                    midlet.exit();
                } else {
                    showDialog = false;
                }
            } else if (btnSearchSd.isIsPressed()) {
                //midlet.showSearchCanvas();
            } else if (btnAboutId.isIsPressed()) {
                //midlet.showOptionCanvas();
            } else if (btnExitSd.isIsPressed()) {
                showDialog = true;
                btnNo.setIsPressed(true);
                btnNo.setIsNothing(false);
                btnNo.setIsSelected(false);
            }

        } else if (keyCode == -2 || keyCode == -4) {
            if (showDialog) {
                if (btnYes.isIsPressed()) {
                    btnYes.setIsPressed(false);
                    btnNo.setIsPressed(true);
                    btnYes.setIsNothing(true);
                }
            } else if (btnAboutId.isIsPressed()) {
                btnAboutId.setIsNothing(true);
                btnAboutId.setIsPressed(false);
                btnSearchSd.setIsPressed(true);
            } else if (btnSearchSd.isIsPressed()) {
                btnSearchSd.setIsNothing(true);
                btnSearchSd.setIsPressed(false);
                btnExitSd.setIsPressed(true);
            } else if (btnExitSd.isIsPressed()) {
                btnExitSd.setIsNothing(true);
                btnExitSd.setIsPressed(false);
                btnAboutId.setIsPressed(true);
            }
        } else if (keyCode == -1 || keyCode == -3) {
            //pencet atas / kiri
            if (showDialog) {
                if (btnNo.isIsPressed()) {
                    btnNo.setIsPressed(false);
                    btnYes.setIsPressed(true);
                    btnNo.setIsNothing(true);
                }
            } else if (btnAboutId.isIsPressed()) {
                btnAboutId.setIsNothing(true);
                btnAboutId.setIsPressed(false);
                btnExitSd.setIsPressed(true);
            } else if (btnSearchSd.isIsPressed()) {
                btnSearchSd.setIsNothing(true);
                btnSearchSd.setIsPressed(false);
                btnAboutId.setIsPressed(true);
            } else if (btnExitSd.isIsPressed()) {
                btnExitSd.setIsNothing(true);
                btnExitSd.setIsPressed(false);
                btnSearchSd.setIsPressed(true);
            }
        } else if (keyCode == -7) {
            showDialog = true;
            btnNo.setIsPressed(true);
            btnNo.setIsNothing(false);
            btnNo.setIsSelected(false);
        } else if (keyCode == -6) {
        }
        repaint();
    }

    protected void pointerPressed(int x, int y) {
        //recognizer = new GestureRecognizer(x, y);
        this.x = x;
        this.y = y;

        //show = false;
        if (showDialog) {
            if (x > btnNo.xImage
                    && x < (btnNo.xImage + btnNo.imageWidth)
                    && y > btnNo.yImage
                    && y < (btnNo.yImage + btnNo.imageHeight)) {
                btnNo.pointerPressed(x, y);
                repaint();
            } else if (x > btnYes.xImage
                    && x < (btnYes.xImage + btnYes.imageWidth)
                    && y > btnYes.yImage
                    && y < (btnYes.yImage + btnYes.imageHeight)) {
                keyPressed(-3);
                btnYes.pointerPressed(x, y);
                repaint();
            }
        } else {
            if (x > btnSelectSd.xImage
                    && x < (btnSelectSd.xImage + btnSelectSd.imageWidth)
                    && y > btnSelectSd.yImage
                    && y < (btnSelectSd.yImage + btnSelectSd.imageHeight)) {
                btnSelectSd.pointerPressed(x, y);
                repaint();
            } else if (x > btnExitSd.xImage
                    && x < (btnExitSd.xImage + btnExitSd.imageWidth)
                    && y > btnExitSd.yImage
                    && y < (btnExitSd.yImage + btnExitSd.imageHeight)) {
                btnExitSd.pointerPressed(x, y);
                repaint();
            } else if (x > btnAboutId.xImage
                    && x < (btnAboutId.xImage + btnAboutId.imageWidth)
                    && y > btnAboutId.yImage
                    && y < (btnAboutId.yImage + btnAboutId.imageHeight)) {
                btnAboutId.pointerPressed(x, y);
                repaint();
            } else if (x > btnSearchSd.xImage
                    && x < (btnSearchSd.xImage + btnSearchSd.imageWidth)
                    && y > btnSearchSd.yImage
                    && y < (btnSearchSd.yImage + btnSearchSd.imageHeight)) {
                btnSearchSd.pointerPressed(x, y);
                repaint();
            }
        }
    }

    protected void pointerReleased(int x, int y) {
        //recognizer.endGesture(x, y);
        this.x = x;
        this.y = y;
        //show = true;
        if (showDialog) {
            if (x > btnNo.xImage
                    && x < (btnNo.xImage + btnNo.imageWidth)
                    && y > btnNo.yImage
                    && y < (btnNo.yImage + btnNo.imageHeight)) {
                btnNo.pointerReleased(x, y);

                btnAboutId.setIsNothing(true);
                btnAboutId.setIsPressed(false);
                btnSearchSd.setIsNothing(true);
                btnSearchSd.setIsPressed(false);
                btnExitSd.setIsPressed(true);


                showDialog = false;
            } else if (x > btnYes.xImage
                    && x < (btnYes.xImage + btnYes.imageWidth)
                    && y > btnYes.yImage
                    && y < (btnYes.yImage + btnYes.imageHeight)) {
                btnYes.pointerReleased(x, y);
                showDialog = true;
                midlet.exit();
            }
        } else {
            if (x > btnSelectSd.xImage
                    && x < (btnSelectSd.xImage + btnSelectSd.imageWidth)
                    && y > btnSelectSd.yImage
                    && y < (btnSelectSd.yImage + btnSelectSd.imageHeight)) {
                btnSelectSd.pointerReleased(x, y);
                btnSelectSd.setIsPressed(true);
                keyPressed(-5);
            } else if (x > btnExitSd.xImage
                    && x < (btnExitSd.xImage + btnExitSd.imageWidth)
                    && y > btnExitSd.yImage
                    && y < (btnExitSd.yImage + btnExitSd.imageHeight)) {
                btnExitSd.pointerReleased(x, y);

                btnExitSd.setIsPressed(true);
                btnAboutId.setIsNothing(true);
                btnAboutId.setIsPressed(false);
                btnSearchSd.setIsNothing(true);
                btnSearchSd.setIsPressed(false);
                btnExitSd.setIsPressed(true);

                keyPressed(-7);
            } else if (x > btnAboutId.xImage
                    && x < (btnAboutId.xImage + btnAboutId.imageWidth)
                    && y > btnAboutId.yImage
                    && y < (btnAboutId.yImage + btnAboutId.imageHeight)) {
                btnAboutId.pointerReleased(x, y);
                btnAboutId.setIsPressed(true);

                btnSearchSd.setIsNothing(true);
                btnSearchSd.setIsPressed(false);
                btnExitSd.setIsNothing(true);
                btnExitSd.setIsPressed(false);
                keyPressed(-5);
            } else if (x > btnSearchSd.xImage
                    && x < (btnSearchSd.xImage + btnSearchSd.imageWidth)
                    && y > btnSearchSd.yImage
                    && y < (btnSearchSd.yImage + btnSearchSd.imageHeight)) {
                btnSearchSd.pointerReleased(x, y);
                btnSearchSd.setIsPressed(true);

                btnAboutId.setIsNothing(true);
                btnAboutId.setIsPressed(false);
                btnExitSd.setIsNothing(true);
                btnExitSd.setIsPressed(false);

                keyPressed(-5);
            }
        }
        repaint();
    }

    private void drawImgCenter(Graphics g, Image img) {
        if (img != null) {
            g.drawImage(img, getWidth() / 2, (getHeight() - img.getHeight()) / 2, Graphics.HCENTER | Graphics.TOP);
        }
    }
}
